package volatileDemo;

import java.util.concurrent.TimeUnit;

/**
 * @author fei
 * @date 2023/1/16 15:01
 * @description volatile 无原子性例子
 */
public class VolatileNoAtomicDemo {

    public static void main(String[] args) {
        MyNumber myNumber = new MyNumber();
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                for (int j = 1; j <= 1000; j++) {
                    myNumber.addPlusPlus();
                }
            }, String.valueOf(i)).start();
        }

        try{ TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) { e.printStackTrace(); }

        System.out.println(myNumber.number);
    }

}

class MyNumber{

    volatile int number;

    // public synchronized void addPlusPlus(){
    public void addPlusPlus(){
        number++;
    }

}

